fix ordered_logistic higher order ad#3327
Conversation
|
I tried to click tiny cogwheel to ask @WardBrian to review, but accidentally clicked copilot just below the cogwheel and I don't see how can I stop it from reviewing. EDIT: found where to cancel the copilot review |
|
I also checked that the |
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
WardBrian
left a comment
There was a problem hiding this comment.
Looks good to me. I confirmed the added tests fail without these changes, and the code matches the pattern used earlier to compute the primal
Summary
This came up in a Stan model using
laplace_marginal. There is a bug inordered_logistic_lpmfandordered_logistic_glm_lpmfhigher order autodiff.Root cause:
exp(-cut1)andexp(cut1)materializations in the partials block pushed orphanvariwithval == +∞onto the autodiff stack wheny == K(cut1 = −∞) ory == 1(cut2 = +∞).Eigen::selectshort-circuits at the value level (so the value path was fine), butstan::math::grad()walks every vari and runsexp_vari::chain():a.adj() += vi.adj() * vi.val()→0 * ∞ = NaNwhenever a tangent seed is 0 (the typical pattern inlaplace_likelihood::compute_s2). The NaN landed on the val-side adjoint chain, which Laplace reads and returns.Fix: Replace
exp(-cut1)/exp(cut1)with(-cut1.abs()).exp()(always in(0, 1]) and rewrite the select branches to use it for bothinv_logit(−cut1)andinv_logit(−cut2). This matches the existing idiom already used 8 lines above in the value computation (m_log_1p_exp_cut1) — so it's stylistically consistent with the rest of the file.Tests
mix/prob/ordered_logistic_test.cpp— 9 tests pass (6 existing + 3 new). Stashing the fix reproduces 2 of the 3 new tests failing (the third is a value/gradient consistency test that always passes), confirming the new tests catch the bug.mix/prob/ordered_logistic_glm_lpmf_test.cpp— 2 tests pass (1 existing + 1 new).rev/prob/ordered_logistic_test.cpp,prim/prob/ordered_logistic_test.cpp,rev/prob/ordered_logistic_glm_lpmf_test.cpp— all 21 tests pass (no first-order AD regression).test_ordered_logistic_laplace_bug.stannow give−6.10forY_with_topand−6.28forY_no_top. Built-inll_builtin_topis no longer NaN.Release notes
Fixed a bug in ordered_logistic_lpmf and ordered_logistic_glm_lpmf higher order autodiff.
Checklist
Stan Development Team. The fix is copying copying existing working code from another distribution, that is, no creativity in the new code.
By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested
AI Use Disclosure
The bug fix and tests was made using Claude AI Agent. The bug fix follows exactly the working code a few lines above.